package reflect;

import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

public class Test5 {
    public static void main(String[] args) throws URISyntaxException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class cls = Test5.class;
        String packageName = cls.getPackage().getName();
        File dir = new File(
                cls.getResource(".").toURI()
        );
        File[] subs = dir.listFiles(f->f.getName().endsWith(".class"));
        for(File sub:subs){
            String fileName = sub.getName();
            String className = fileName.substring(0,fileName.indexOf("."));
            className = packageName+"."+className;
            Class c = Class.forName(className);
            if(c.isAnnotationPresent(AutoRunClass.class)){
                Object o = c.newInstance();
                System.out.println("被标注的类:"+className);
                System.out.println(o);
                Method[] methods = c.getDeclaredMethods();
                for(Method m:methods){
                    if(m.isAnnotationPresent(AutoRunMethod.class)){
                        m.invoke(o);
                        System.out.println("被标注的方法:"+m.getName());
                    }
                }
            }
        }
    }
}
